﻿using Core.Tools;
using System;

namespace Core.Entities
{
    public sealed class RuntimeParamsSign
    {
        /// <summary>
        /// 构造运行时函数参数签名标识
        /// </summary>
        /// <param name="Name">函数名称</param>
        public RuntimeParamsSign(object[] Params)
        {
            InternalParams = Params;
            HashCode = 46104728 * Params.Length;

            IsInputParams = true;
        }

        /// <summary>
        /// 构造运行时函数参数签名标识
        /// </summary>
        /// <param name="Name">函数名称</param>
        public RuntimeParamsSign(Type[] Types):this((object[])Types)
        {
            IsInputParams = false;
        }

        private readonly object[] InternalParams;
        private readonly bool IsInputParams;
        private readonly int HashCode;

        public override int GetHashCode()
        {
            return HashCode;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is RuntimeParamsSign))
            {
                return false;
            }

            var Object = (RuntimeParamsSign)obj;

            if (InternalParams.Length == Object.InternalParams.Length)
            {
                if (IsInputParams)
                {
                    return TypeHelper.ParamsCompares((Type[])Object.InternalParams, InternalParams);
                }
                else if (Object.IsInputParams)
                {
                    return TypeHelper.ParamsCompares((Type[])InternalParams, Object.InternalParams);
                }
                else
                {
                    return TypeHelper.ParamsCompares((Type[])Object.InternalParams, (Type[])InternalParams);
                }
            }

            return false;


        }
    }
}
